<?php

class RedisDB
{
    protected $redis = null;
    protected $prefix = "test";
    protected $total_key = 'test_hash_total'; // 记录插入的总数据量
    protected $key_prefix = 'test_key_';      // 存放哈希表的键
    protected $key_status = 'test_status_';   // 用于判断uid是否已存在

    // 爬取到的uid存入队列
    // 并从队列中弹出uid，让爬虫去爬取这个uid的粉丝和关注列表
    protected $queue = "test_uid_list"; 

    public function __construct(){
        try{
            $this->redis = new Redis();
            $this->redis->connect("localhost", 6379);     
        }catch(Exception $e){
            exit("连接redis数据库失败");
        }
       
    }

    /**
     * @title 将uid放入队列中
     * @param $uid 
     * @return int|false  成功则返回队列长度，失败返回false
     */
    public function pushUid($uid){
        try{
            $res = $this->redis->lPush($this->queue, $uid);
        }catch(Exception $e){
            echo $e->getMessage(), PHP_EOL;
        }
        return $res;
    }

    /**
     * @title 从队列中弹出一个uid
     * @return string|false  成功返回uid，失败返回false
     */
    public function popUid(){
        try{
            $res = $this->redis->rPop($this->queue);
        }catch(Exception $e){
            echo $e->getMessage(), PHP_EOL;
        }
        return $res;
    }

    /**
     * @title 返回队列中uid数量
     * @return int | false
     */
    public function getSize(){
        try{
            $res = $this->redis->lSize($this->queue);
        }catch(Exception $e){
            echo $e->getMessage(), PHP_EOL;
        }
        return $res;
    }

    /**
     * @title 判断该uid是否存在了
     * return bool
     */
    public function keyIsExists($uid){
        try{
            $key = $this->key_status . $uid;
            $res = $this->redis->exists($key);
        }catch(Exception $e){
            echo $e->getMessage(),PHP_EOL;
        }
        
        return $res;
    }

    /**
     * @title 设置该uid为已存在
     * return bool
     */
    public function keySetExists($uid){
        try{
            $key = $this->key_status . $uid;      
            $res = $this->redis->set($key,1);
        }catch(Exception $e){
            echo $e->getMessage(),PHP_EOL;
        }
        
        return $res;  // 成功返回true
    }

    /**
     * @title 保存用户信息
     * return bool
     */
    public function saveInfo($uid, $info){
        $key = $this->prefix  .'_hash_'. $uid;
        try{
            if(is_array($info)){     
                $res = $this->redis->hMset($key, $info);
                echo "测试：",$res,PHP_EOL;
                if($res){
                    
                    $cur_number = $this->redis->incr($this->total_key);  // 总数自增1，并返回自增后的值
                    $cur_key = $this->key_prefix . $cur_number;
                    $this->redis->set($cur_key, $key);    // 存放当前的键，方便获取键的名称
                    return $res;
                }
            }
        }catch(Exception $e){
            echo $e->getMessage(),"__LINE__";
        }   
        return false;
    }

    /**
     * @title 获取用户信息
     * return array
     */
    public function getInfo($key){
        // $key = $this->prefix  .'_hash_'. $uid;
        $field = [
            'uid',
            'username',
            'code_age',
            'raw_count',
            'fans_count',
            'hot_count',
            'comment_count',
            'visit_count',
            'jifen_count',
            'collect_count',
            'week_ranking',
            'total_ranking',
            'blog_level'
        ];
        try{
            $res = $this->redis->hMget($key, $field);
        }catch(Exception $e){
            echo $e->getMessage(),PHP_EOL;
        }
        return $res;
    }

    /**
     * @title获取存在redis的用户信息总数
     */
    public function getTotal(){
        try{
            $res = $this->redis->get($this->total_key);
        }catch(Exception $e){
            echo $e->getMessage(),PHP_EOL;
        }
        return $res;
    }

    /**
     * @title 根据索引返回uid
     */
    public function getUid($index){
        try{
            $res = $this->redis->get($this->key_prefix . $index);
        }catch(Exception $e){
            echo $e->getMessage(),PHP_EOL;
        }
        return $res;
    }
}
